package heman.server.entities;
/**
 * All helper functions of Player
 * 
 * @author Wenlong Liu
 */


import java.util.ArrayList;


public class Player {
	String name=null;
	String id=null;
	ArrayList<Rating> ratings=new ArrayList<Rating>();
	
	public String getId() {
		return id;
	}
	public Player(String id) {
		super();
		this.id = id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//Be careful. This only add the rating objects to the player
	public void addRating(String cat, int value){
		ratings.add(new Rating(cat, value));
	}
	//This will increase the number of wins of a player
	public boolean increaseRating(String cat){
		
		synchronized (ratings) {
			for (Rating r:ratings)
				if (r.getCat().equals(cat)){
					r.setValue(r.getValue()+1);
					return true;
				}
				
		} 
		return false;
	}
	public void updateRating(String cat,int newVal){
		
		for (Rating rating:ratings) {
			if (rating.getCat()==cat)
				rating.setValue(newVal);
		}
	}
	public ArrayList<Rating> getRatings(){
		return ratings;
	}
	public void setRatings(ArrayList<Rating> r){
		ratings=r;
	}
	
	/**
	 * 
	 * @param game The game that the player is currently playing 
	 * @return The String of an XML Node
	 *  <xs:element name="player-id">
	- <xs:complexType>
	  <xs:attribute name="player" type="valid-player-id" use="required" /> 
	  <xs:attribute name="rating" type="xs:int" use="optional" /> 
	  </xs:complexType>
	  </xs:element>
	 */
	public String toXMLPlayerID(String game){
		StringBuilder sb=new StringBuilder();
		sb.append("<player-id player=\"");
		sb.append(id);
		sb.append("\"");
		//Find the rating
		if (game!=null){
			for (Rating rating2:ratings){
				if (rating2.getCat()==game){
					sb.append(" rating=\"");
					sb.append(rating2.getValue());
					sb.append("\"");
				}
			}
		}
		sb.append("/>");
		return sb.toString();
				
	}
	
	
	/**
	 * 
	 * This returns the XML node of "player"
	 * 
	 * 
	 * @param game The game that the player is currently playing 
	 * @return The String of an XML Node
	 *  <xs:element name="player">
		- <xs:complexType>
		- <xs:sequence>
		  <xs:element ref="rating" minOccurs="0" maxOccurs="unbounded" /> 
		  </xs:sequence>
		  <xs:attribute name="player" type="valid-player-id" use="required" /> 
		  <xs:attribute name="realName" type="xs:string" use="optional" /> 
		  </xs:complexType>
		  </xs:element>

	 */
	public String toXMLPlayer(){
		StringBuilder sb=new StringBuilder();
		sb.append("<player player=\"");
		sb.append(id);
		sb.append("\"");
		sb.append(" realName=\"");
		if (name!=null)
			
			sb.append(name);
		sb.append("\">");
		//Add all ratings of the player
		for (Rating rating:ratings) {
			sb.append(rating.toXML());
		}
		sb.append("</player>");
		//Debug.print(sb.toString());
		return sb.toString();
				
	}
	
}
